package com.yf.serviceImpl;

import com.yf.bean.*;
import com.yf.dao.*;
import com.yf.entity.BreakpointEntity;
import com.yf.service.RedisService;
import com.yf.service.UserService;
import com.yf.utils.Constant;
import com.yf.utils.UserContext;
import com.yf.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("UserServiceImpl")
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    SolveLogDao logDao;
    @Autowired
    CollectDao collectDao;
    @Autowired
    BreakpointDao breakpointDao;

    @Autowired
    RedisService redisService;

    @Autowired
    HistoryAnswerDao historyAnswerDao;

    @Autowired
    UserKickLogDao userKickLogDao;

    @Override
    public UserBean login(UserBean user) {
        UserBean userExisted = userDao.getByEmail(user);
        if (userExisted == null) {
            return null;
        }
        if (!user.getPasswd().equals(userExisted.getPasswd())) {
            return null;
        }
        userExisted.setLastLoginTime(Utils.currentTime());
        userDao.updateUserLoginTime(userExisted);
        return userExisted;
    }


    @Override
    public int register(UserBean user) {
//        if (userDao.getByEmail(user) != null) {
//            return -2;
//        }
        return userDao.addUser(user);
    }


    @Override
    public boolean logout(UserBean user) {
        return false;
    }

    @Override
    public LogStatisticBean getLogStatistic(Integer uid) {
        return logDao.getSolveNum((long) uid);
    }

    @Override
    public int addLogStatistic(List<SolveLogBean> logs) {
        return logDao.addLog(logs);
    }

    @Override
    public int activateUser(Integer uid) {
        return userDao.activateUser(uid);
    }

    @Override
    public boolean existEmail(Integer emailID) {
        return userDao.existEmail(emailID);
    }

    @Override
    public boolean existUser(Integer nameID) {
        return userDao.existUsername(nameID);
    }

    @Override
    public int addCollect(List<CollectionBean> data) {
        return collectDao.addCollect(data);
    }

    @Override
    public int deleteCollect(Integer id) {
        Map<String, Object> params = new HashMap<>(2);
        final Integer userId = UserContext.getUserId();
        if (userId <= 0) {
            return -1;
        }
        params.put("id", id);
        params.put("uid", userId);
        return collectDao.deleteById(params);
    }

    @Override
    public List<CollectionBean> selectCollect(Integer uid) {
        return collectDao.selectCollect(uid);
    }

    @Override
    public List<CollectionBean> getCollectBySub(Integer uid, Integer cate) {
        Map<String, Object> params = new HashMap<>(2);
        params.put("uid", uid);
        params.put("cate", cate);
        return collectDao.getCollectByUidSub(params);
    }

    @Override
    public int markBlackUser(Long uid) {
        return userDao.markBlackUser(uid);
    }

    @Override
    public Integer getUserPriority(Integer uid) {
        return userDao.getUserPriority(uid);
    }

    @Override
    public Boolean addBreakpoint(BreakpointBean breakpoint) {
        Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return false;
        }
        breakpoint.setUid(uid);
        Integer id = breakpoint.getId();
        if (id != null && id > 0) {
            breakpoint.setCreated(Utils.currentTime());
            return breakpointDao.updateBreakpoint(breakpoint) > 0;
        }
        List<BreakpointBean> bk = breakpointDao.getBreakpointByUid(breakpoint);
        if (bk != null && bk.size() > 0) {
            BreakpointBean breakpointBean = bk.get(0);
            breakpointBean.setCreated(Utils.currentTime());
            breakpointBean.setCategory(breakpoint.getCategory());
            breakpointBean.setIdx(breakpoint.getIdx());
            breakpointBean.setPid(breakpoint.getPid());
            return breakpointDao.updateBreakpoint(breakpointBean) > 0;
        } else {
            breakpoint.setCreated(Utils.currentTime());
            return breakpointDao.addBreakpoint(breakpoint) > 0;
        }
    }

    @Override
    public List<BreakpointEntity> getUserBreakpoint() {
        Integer uid = UserContext.getUserId();
        if (uid <= 0) {
            return null;
        }
        BreakpointBean breakpoint = new BreakpointBean();
        breakpoint.setUid(uid);
        return breakpointDao.getBreakpointByUidV2(breakpoint);
    }

    @Override
    public Boolean resetUserPass(UserBean bean) {
        return userDao.resetUserPass(bean) > 0;
    }

    @Override
    public Boolean resetUserName(UserBean bean) {
        return userDao.resetUsername(bean) > 0;
    }

    @Override
    public String getUserReviewList() {
        final Integer uid = UserContext.getUserId();
        if (uid > 0) {
            return redisService.getUserInfoLong(uid, Constant.USER_REVIEW_KEY);
        }
        return "";
    }

    @Override
    public List<HistoryAnswer> getUserHistoryAnswer(Integer uid, Integer subject) {
        Map<String, Object> params = new HashMap<>();
        params.put("uid", uid);
        params.put("cate", subject);
        return historyAnswerDao.getByUid(params);
    }

    @Override
    public UserKickLog getUserKickStat(Integer uid) {
        return userKickLogDao.getUserKickStat(uid);
    }
}
